package lc20240504;

import java.util.HashSet;
import java.util.Scanner;

//快乐数
public class Demo2 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        System.out.println("isHappy("+n+")=" + isHappy(n));
    }
    public static boolean isHappy(int n){
        HashSet<Integer> set=new HashSet<>();
        //n如果为0，跳出循环。
        while (n!=0&&!set.contains(n)){
            set.add(n);
            n=compute(n);
        }
        return n==1;
    }
    public static int compute(int n){
        int con=n;
        int remain=0;
        int sum=0;
        while(con!=0){
            remain=con%10;
            con/=10;
            sum+=remain*remain;
        }
        return sum;
    }
}
